package com.example.finalapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SecondFragment extends Fragment implements Runnable, AdapterView.OnItemClickListener {
    private static final String TAG = "SecondFragment";
    Handler handler;
    ListView listview;
    ArrayList<HashMap<String, String>> list;
    ProgressBar bar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second,container,false);

        listview = view.findViewById(R.id.mylist2);
        bar = view.findViewById(R.id.progressBar);
        listview.setEmptyView(bar);

            handler = new Handler(Looper.myLooper()) {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    //收到数据
                    if (msg.what == 7) {
                        List<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                        //构造Adapter
                        SimpleAdapter ListItemAdapter = new SimpleAdapter(getActivity(),
                                list,
                                R.layout.list_item,
                                new String[]{"cname", "cval","cname1","cval1"},
                                new int[]{R.id.title, R.id.detail,R.id.title1,R.id.detail1});

                              listview.setAdapter(ListItemAdapter);
                    }
                    super.handleMessage(msg);
                }
            };
            Thread t = new Thread(this);
            t.start();

            return view;
        }


    public void run() {
        //获取网络数据
        List<HashMap<String, String>> relist = new ArrayList<HashMap<String, String>>();
        try {
            Log.i(TAG, "run: running......");
            Document doc = Jsoup.connect("http://www.yacou.net/yy/yy2/4-rcyd/index.htm").get();
            Log.i(TAG, "run: title=");

            Elements tables = doc.getElementsByTag("table");
            Element table = tables.get(7);
            Elements trs = table.getElementsByTag("tr");
            for (int i=2;i<20;i++) {
                Element tr = trs.get(i);
                Elements tds = tr.getElementsByTag("td");
                if (tds.size()>0) {
                    String td1 = tds.get(0).text();
                    String td2 = tds.get(1).text();
                    String td3 = tds.get(2).text();
                    String td4 = tds.get(3).text();
                    Log.i(TAG, "run: " + td1 + "-->" + td2);
                    Log.i(TAG, "run: " + td3 + "-->" + td4);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("cname", td1);
                    map.put("cval", td2);
                    map.put("cname1", td3);
                    map.put("cval1", td4);
                    relist.add(map);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage(7, relist);
        //    msg.what = 7;
        //    msg.obj = "hello from msg";
        handler.sendMessage(msg);
        Log.i(TAG, "run: sendMessage OK");
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

}